package Datos;
// Generated 15-dic-2012 9:31:07 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Set;
import java.util.Vector;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Cargo generated by hbm2java
 */
public class Cargo  implements java.io.Serializable {


     private int item;
     private String nombre;
     private int nivel;
     private Float monto;
private Session session;
    public Cargo() {
    }

	
    public Cargo(int item, String nombre, int nivel) {
        this.item = item;
        this.nombre = nombre;
        this.nivel = nivel;
    }
    public Cargo(int item, String nombre, int nivel, Float monto) {
       this.item = item;
       this.nombre = nombre;
       this.nivel = nivel;
       this.monto = monto;
    }
   
    public int getItem() {
        return this.item;
    }
    
    public void setItem(int item) {
        this.item = item;
    }
    public String getNombre() {
        return this.nombre;
    }
    
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public int getNivel() {
        return this.nivel;
    }
    
    public void setNivel(int nivel) {
        this.nivel = nivel;
    }
    public Float getMonto() {
        return this.monto;
    }
    
    public void setMonto(Float monto) {
        this.monto = monto;
    }

    //-------------------------------------------------------------------------
    public int cantTuplas() {
        return HibernateUtil.CantTuplas(resultado());
    }
 //--------------------------------------------------------------------------//
    public void insertar() {

        try {
  
            Session Sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = Sesion.beginTransaction();
            Sesion.save(this);
            tx.commit();
            Sesion.close();
            JOptionPane.showMessageDialog(null, "SE INSERTARON LOS DATOS CORRECTAMENTE EN LA BD.", "MENSAJE", JOptionPane.WARNING_MESSAGE, null);

        } catch (HibernateException e) {
            System.out.println(" Error al INSERTAR  " + e.getMessage());
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }

    }
//--------------------------------------------------------------------------//

    public void modificar() {

        try {

            Session Sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = Sesion.beginTransaction();
            Sesion.update(this);
            tx.commit();
            Sesion.close();
            JOptionPane.showMessageDialog(null, "SE MODIFICARON LOS DATOS CORRECTAMENTE EN LA BD.", "MENSAJE", JOptionPane.WARNING_MESSAGE, null);

        } catch (HibernateException e) {
            System.out.println("Error al MODIFICAR  " + e.getMessage());
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }
    }
//-----------------------------------------------------------------------------//

    public void eliminar() {

        try {

            Session Sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = Sesion.beginTransaction();
            Cargo t = getCargo();
            Sesion.delete(t);
            tx.commit();
            Sesion.close();
            JOptionPane.showMessageDialog(null, "SE ELIMINARON LOS DATOS CORRECTAMENTE EN LA BD.", "MENSAJE", JOptionPane.WARNING_MESSAGE, null);

        } catch (HibernateException e) {
            System.out.println("Error al ELIMINAR  " + e.getMessage());
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }
    }
//-----------------------------------------------------------------------------//

    public boolean existeCargo(String nombre) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;
        try {
            String query = "SELECT * FROM Cargo "
                    + "WHERE "
                    + "nombre = '" + nombre + "';";
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();

        } catch (Exception e) {
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return HibernateUtil.existe(rs);
    }

//--------------------------------------------------------------------------//

    public boolean existeID(int item) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;
        try {
            String query = "SELECT * FROM Cargo "
                    + "WHERE "
                    + "item = " + item + "";
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();

        } catch (Exception e) {
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return HibernateUtil.existe(rs);
    }

//--------------------------------------------------------------------------//

    public ResultSet EjecutarConsultaSQL(String sql) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;

        try {
            String query = sql;
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();
        } catch (Exception e) {
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return rs;
    }

//-----------------------------------------------------------------------------//

    public String listar() {
        ResultSet rs = EjecutarConsultaSQL("select *from Cargo order by item;");

        return HibernateUtil.mostrarTuplas(rs, "Lista de Cargo", new String[]{"ITEM", "NOMBRE","NIVEL","MONTO"},
                new String[]{"item", "nombre","nivel","monto"});
    }

//-----------------------------------------------------------------------------//

    public ResultSet resultado() {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null; 

        try {
            String query = "SELECT * FROM Cargo order by item;";
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();

        } catch (Exception e) {
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return rs;
    }
//-----------------------------------------------------------------------------//

    public Cargo getCargo() {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.getTransaction();
        try {

            tx.begin();
            Cargo t = (Cargo) session.load(Cargo.class, getItem());
            return t;
        } catch (Exception e) {
            tx.rollback();
            Logger.getLogger(Cargo.class.getName()).log(Level.SEVERE, null, e);
        }
        return null;
    }

//--------------------------------------------------------------------------//

}